package net.vkits.common.listeners;

import net.vkits.anjiamiao.domain.model.ComplainStatus;
import net.vkits.anjiamiao.domain.model.Message;
import net.vkits.anjiamiao.domain.model.TradeStatus;
import net.vkits.common.push.PushService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;

/**
 * Created by ethan-liu on 2017/1/7.
 */
@Component
public class MessageListener {
    private static final Logger logger = LoggerFactory.getLogger(MessageListener.class);

    @Resource
    private PushService pushService;

    @TransactionalEventListener
    public void handleMessage(Message message) {
        String deviceTokens = StringUtils.join(message.getUmDeviceTokens().toArray(), ",");
        logger.info("Message is: {}", message);
        // 已转账
        if (message.getStatus() == TradeStatus.TransferSuccess) {
            String temp = message.getRentAmount()
                    + "元转入房东"
                    + message.getBank()
                    + "账户"
                    + message.getAccount()
                    + "银行（尾号）已成功！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "房租已成功转给房东，更多详情打开app",
                    getCustom(message, temp));
            // 转账失败
        } else if (message.getStatus() == TradeStatus.TransferFailed) {
            String temp = message.getRentAmount()
                    + "元转入房东"
                    + message.getBank()
                    + "账户"
                    + message.getAccount()
                    + "银行（尾号）已失败！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "房租转账失败，房东信息有误，更多详情打开app",
                    getCustom(message, temp));
            // 转账失败退款
        } else if (message.getStatus() == TradeStatus.TransferFailedRefund) {
            String temp = message.getRentAmount()
                    + "元房租（含服务费）已退回！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "您的房租已原款退回，更多详情打开app",
                    getCustom(message, temp));
            // 风险交易
        } else if (message.getStatus() == TradeStatus.RiskTrade) {
            String temp = "系统认定为风险交易，转账已中止！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "您有一笔风险交易被中止，更多详情打开app",
                    getCustom(message, temp));
            // 风险交易退款
        } else if (message.getStatus() == TradeStatus.RiskTradeRefund) {
            String temp = message.getRentAmount()
                    + "元房租（含服务费）已退回！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "您的房租已原款退回，更多详情打开app",
                    getCustom(message, temp));
            // 处理中，支付宝支付成功
        } else if (message.getStatus() == TradeStatus.Paid) {
            String temp = message.getRentAmount()
                    + "元转入房东"
                    + message.getBank()
                    + "账户"
                    + message.getAccount()
                    + "银行（尾号）处理中！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵最新到账进度来了！",
                    message.getTitle(),
                    "房租支付成功，正在转账处理，请耐心等待。",
                    getCustom(message, temp));
        }
        // 提交申诉
        if (message.getComplainStatus() == ComplainStatus.Submit) {
            String temp = "您提交的申诉正在处理中！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "申诉已成功提交给安家喵",
                    message.getTitle(),
                    "您的申诉已提交给安家喵，系统正在审核，请耐心等待。",
                    getCustom4Complain(message, temp));
            // 申诉通过
        } else if (message.getComplainStatus() == ComplainStatus.Accept) {
            String temp = "您的申诉理由已通过审核！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵申诉审核有结果了！",
                    message.getTitle(),
                    "您的申诉已通过审核，可以正常交租，更多详情打开app",
                    getCustom4Complain(message, temp));
            // 申诉不通过
        } else if (message.getComplainStatus() == ComplainStatus.Unaccepted) {
            String temp = "您的申诉理由未通过审核！";
            pushService.pushCustom2Android(
                    deviceTokens,
                    "安家喵申诉审核有结果了！",
                    message.getTitle(),
                    "您的申诉未通过审核，暂时无法交租，更多详情打开app",
                    getCustom4Complain(message, temp));
        }
    }

    private String getCustom(Message message, String temp) {
        return "{\"title\":\""
                + message.getTitle()
                + "\",\"CreateDate\":\""
                + message.getCreateDate()
                + "\",\"url\":\""
                + "http://api.anlewowo.com/h5/orderStatus/" + message.getId()
                + "\",\"text\":\""
                + temp
                + "\"}";
    }

    private String getCustom4Complain(Message message, String temp) {
        return "{\"title\":\""
                + message.getTitle()
                + "\",\"CreateDate\":\""
                + message.getCreateDate()
                + "\",\"url\":\""
                + "http://api.anlewowo.com/h5/appeals/" + message.getId()
                + "\",\"text\":\""
                + temp
                + "\"}";
    }
}
